package com.change360.calendarview;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.RippleDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.StateListDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.media.Image;
import android.os.Build;
import android.support.annotation.NonNull;
import android.text.SpannableString;
import android.text.Spanned;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckedTextView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

/**
 * Created by 年高 on 2015/6/17.
 */
public class DayView extends LinearLayout {

    private CalendarDay date;
    private int selectionColor = Color.GRAY;

    private final int fadeTime;
    private Drawable customBackground = null;
    private Drawable selectionDrawable;
    TextView text;
    ImageView icon;
    private boolean checked;

    public DayView(Context context, CalendarDay day) {
        super(context);

        LayoutInflater.from(context).inflate(R.layout.widget_day, this);
        text = (TextView) findViewById(R.id.text);
        icon = (ImageView) findViewById(R.id.icon);
        icon.setVisibility(View.GONE);
        fadeTime = getResources().getInteger(android.R.integer.config_shortAnimTime);

        setSelectionColor(this.selectionColor);

        setGravity(Gravity.CENTER);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            setTextAlignment(TEXT_ALIGNMENT_CENTER);
        }
        setBackgroundColor(getResources().getColor(R.color.titletextwhite));
        setDay(day);
    }

    public void setDay(CalendarDay date) {
        this.date = date;
        text.setText(getLabel());
    }

    public
    @NonNull
    String getLabel() {
        return String.valueOf(date.getDay());
    }

    public void setSelectionColor(int color) {
        this.selectionColor = color;
        regenerateBackground();
    }

    /**
     * @param selectionDrawable custom selection drawable
     */
    public void setSelectionDrawable(Drawable selectionDrawable) {
        this.selectionDrawable = selectionDrawable;
        invalidate();
    }

    /**
     * @param customBackground background to draw behind everything else
     */
    public void setCustomBackground(Drawable customBackground) {
        this.customBackground = customBackground;
        invalidate();
    }

    public CalendarDay getDate() {
        return date;
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
    }

    protected void setupSelection(boolean showOtherDates, boolean inRange, boolean inMonth) {
        boolean enabled = inMonth && inRange;
        setEnabled(enabled);
        //setVisibility(enabled || showOtherDates ? View.VISIBLE : View.INVISIBLE);
        if (enabled || showOtherDates) {
            text.setTextColor(getResources().getColor(R.color.headtextcolor));
        } else {
            text.setTextColor(getResources().getColor(R.color.titlebg));

        }


    }

    private final Rect tempRect = new Rect();

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
//        if(customBackground != null) {
//            canvas.getClipBounds(tempRect);
//            customBackground.setBounds(tempRect);
//            customBackground.setState(getDrawableState());
//            customBackground.draw(canvas);
//        }
        super.onDraw(canvas);
    }

    private void regenerateBackground() {
//        if(selectionDrawable != null) {
//            setBackgroundDrawable(selectionDrawable);
//        }
//        else {
//            setBackgroundDrawable(generateBackground(selectionColor, fadeTime));
//        }
    }

    private static Drawable generateBackground(int color, int fadeTime) {
        StateListDrawable drawable = new StateListDrawable();
        drawable.setExitFadeDuration(fadeTime);
        drawable.addState(new int[]{android.R.attr.state_checked}, generateCircleDrawable(color));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            drawable.addState(new int[]{android.R.attr.state_pressed}, generateRippleDrawable(color));
        } else {
            drawable.addState(new int[]{android.R.attr.state_pressed}, generateCircleDrawable(color));
        }

        drawable.addState(new int[]{}, generateCircleDrawable(Color.TRANSPARENT));

        return drawable;
    }

    private static Drawable generateCircleDrawable(final int color) {
        ShapeDrawable drawable = new ShapeDrawable(new OvalShape());
        drawable.setShaderFactory(new ShapeDrawable.ShaderFactory() {
            @Override
            public Shader resize(int width, int height) {
                return new LinearGradient(0, 0, 0, 0, color, color, Shader.TileMode.REPEAT);
            }
        });
        return drawable;
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private static Drawable generateRippleDrawable(final int color) {
        ColorStateList list = ColorStateList.valueOf(color);
        Drawable mask = generateCircleDrawable(Color.WHITE);
        return new RippleDrawable(list, null, mask);
    }

    /**
     * @param facade apply the facade to us
     */
    void applyFacade(DayViewFacade facade) {
        setCustomBackground(facade.getBackgroundDrawable());
        setSelectionDrawable(facade.getSelectionDrawable());

        // Facade has spans
        if (facade.getDrawable() != null) {
            icon.setVisibility(View.VISIBLE);
            icon.setImageDrawable(facade.getDrawable());
        }
        // Reset in case it was customized previously
        else {
            icon.setVisibility(View.INVISIBLE);
            text.setText(getLabel());
        }
    }

    public void setTextAppearance(Context context, int taId) {

    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public void setChecked(boolean checked) {
        this.checked = checked;
        if(checked){
            //setBackgroundColor(getResources().getColor(R.color.titlebg));
            //setBackgroundDrawable(getResources().getDrawable(R.drawable.bgbox));
            setBackground(getResources().getDrawable(R.drawable.bgbox));
        }else{
            setBackgroundColor(getResources().getColor(R.color.titletextwhite));
        }
    }
}
